package com.adnaan.restaurant.controller;

import com.adnaan.restaurant.common.untils.R;
import com.adnaan.restaurant.entity.ResAddress;
import com.adnaan.restaurant.service.IResAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * author: adnaan
 * create: 2023-12-10 12:30
 * Description:
 */

@Tag(name = "收货地址接口", description = "用户收货地址增删改查")
@RestController
@RequestMapping("/restaurant/address")
public class ResAddressController {

    @Autowired
    private IResAddressService resAddressService;


    @Operation(summary = "新增地址")
    @PostMapping("/add")
    public R add(@RequestBody ResAddress resAddress) {
        int insertStatus = resAddressService.insertAddress(resAddress);
        if (insertStatus > 0) {
            return R.success("添加成功");
        } else {
            return R.error(500, "添加失败");
        }
    }

    @Operation(summary = "删除地址")
    @DeleteMapping("/del")
    public R del(@RequestParam Long id) {
        int delStatus = resAddressService.deleteAddress(id);
        if (delStatus > 0) {
            return R.success("删除成功");
        } else {
            return R.error(500, "删除失败");
        }
    }

    @Operation(summary = "修改地址")
    @PutMapping("/update")
    public R update(@RequestBody ResAddress resAddress) {
        int updateStatus = resAddressService.updateAddress(resAddress);
        if (updateStatus > 0) {
            return R.success("更新成功");
        } else {
            return R.error(500, "更新失败");
        }
    }

    @Operation(summary = "地址列表")
    @GetMapping("/findAll")
    public R findAll(@RequestParam Long userId) {
        List<ResAddress> list = resAddressService.selectAddressListByuserId(userId);
        return R.success(list);
    }
}
